// pages/crowdraising/iwantcrowd/iwantcrowd.js
var utils = require('../../../utils/util.js');
var WxParse = require('../../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isback: false,
        currentBut: 0,
        isHide: true //支付弹窗是否显示
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        const res = wx.getSystemInfoSync();
        that.setData({
            id: options.id,
            windowHeight: res.windowHeight - 44
        })
        that.crowdDetail();
    },
    crowdDetail: function(e) {
        var that = this;
        utils.http("/api/v1/Crowdgoods/goods_detail", {
            id: that.data.id
        }, function(res) {
            WxParse.wxParse('article', 'html', res.datas.content, that, 5);
            res.datas.number = 0;
            that.setData({
                data: res.datas
            })

        })
    },
    //加数量
    onPrevTap: function(e) {
        var that = this;
        var itemslist = that.data.data;
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.number;
        itemslist.number = itemslist.number + 1;
        that.setData({
            data: itemslist
        })
    },
    //支付方式
    paymentTypeTap: function(e) {
        var id = e.currentTarget.dataset.id;
        var currentBut = id;
        this.setData({
            currentBut: currentBut,
            type: e.currentTarget.dataset.type
        })
    },
    //支付成功
    onOrderSuccessTap: function(e) {
        var that = this;
        console.log(that.data.type);
        if (that.data.type == "wx_pay") {
            utils.http("/api/v1/member/add_vip_order", {
                vip_id: that.data.vipId,
                peyment_code: that.data.type
            }, function(res) {
                if (res.state == 1) {
                    wx.requestPayment({
                        timeStamp: res.datas.timeStamp,
                        nonceStr: res.datas.nonceStr,
                        package: '',
                        signType: res.datas.signType,
                        paySign: res.datas.paySign,
                        success(res) {
                            //支付成功
                            wx.navigateTo({
                                url: '../../ordersuccess/ordersuccess'
                            })

                        },
                        fail(res) {}
                    })
                }
            })
        }
        if (that.data.type == "balance") {
            utils.http("/api/v1/member/add_vip_order", {
                vip_id: that.data.vipId,
                peyment_code: that.data.type
            }, function(res) {
                if (res.state == 1) {
                    //支付成功
                    wx.navigateTo({
                        url: '../../ordersuccess/ordersuccess'
                    })


                }
            })
        }
    },
    //立即购买
    buyNowTap: function (e) {
        //没有选择自提   送货上门
        wx.navigateTo({
            url: '../../confirmorder/confirmorder?type=1',
        })
    },
    //去逛逛
    onGogoTap: function(e) {
        wx.navigateBack({
            delta: 1,
        })
    },
    //减数量
    onNextTap: function(e) {
        var that = this;
        var itemslist = that.data.data;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        if (itemslist.number > 0) {
            itemslist.number = itemslist.number - 1;
            that.setData({
                data: itemslist
            })
        } else {
            utils.message("该宝贝宝贝不能减少哦~")
        }

    },
    //点击我要筹出现弹窗
    onBackColseTap: function(e) {
        var that = this;
        that.setData({
            isback: true
        })
    },
    //关闭我要筹
    onClosebackTap: function(e) {
        var that = this;
        that.setData({
            isback: false
        })
    },
    //关闭支付弹窗
    onColseTap: function(e) {
        this.setData({
            isHide: true
        })
    },
    //打开支付弹窗
    onOrderTap: function(e) {
        this.setData({
            isHide: false,
            isback: false
        })
    },
    //店铺单项选择 （选择自提）
    bindButTap: function(e) {
        console.log(e.currentTarget.dataset.current);
        var id = e.currentTarget.dataset.current;
        this.setData({
            currentBut: id
        })
    },

})